package com.sky.controller;

import com.sky.entity.TWallet;
import com.sky.entity.TWalletRecord;
import com.sky.service.TWalletRecordService;
import com.sky.service.TWalletService;
import com.sky.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * (TWallet)表控制层
 *
 * @author sky
 * @since 2022-11-15 11:42:54
 */
@RestController
@RequestMapping("/tWallet")
public class TWalletController {
    /**
     * 服务对象
     */
    @Resource
    private TWalletService tWalletService;

    @Resource
    private TWalletRecordService tWalletRecordService;

    /**
     * 通过用户主键查询钱包余额
     * @param userId 主键
     * @return 单条数据
     */
    @GetMapping("/getWalletMoney")
    public Object queryWalletMoneyById(@RequestParam("userId") Integer userId) {
        BigDecimal walletMoney = tWalletService.queryWalletMoneyById(userId);
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("status", "Success");
        obj.put("content", walletMoney);
        return obj;
    }

    /**
     * 用户消费
     * @param userId
     * @param money
     * @param orderId
     * @param bankNumber
     * @return
     */
    @Transactional
    @PostMapping("/consume")
    public Object consume(@RequestParam("userId") long userId,
                          @RequestParam("money") BigDecimal money,
                          @RequestParam("orderId") Long orderId,
                          @RequestParam("bankNumber") Integer bankNumber) {
        Map<String, Object> obj = new HashMap<String, Object>();
        // 用户消费
        boolean walletResult = tWalletService.consume(userId, money);
        if (!walletResult) {
            obj.put("status", "fail");
            obj.put("content", "交易失败，余额不足");
            return obj;
        }
        // 消费明细
        TWallet tWallet = tWalletService.queryById(userId);
        TWalletRecord tWalletRecord = new TWalletRecord();
        tWalletRecord.setWalletId(tWallet.getWalletId());
        tWalletRecord.setUserId(userId);
        tWalletRecord.setOrderId(orderId);
        tWalletRecord.setAmount(tWallet.getWalletAmount());
        tWalletRecord.setOldAmount(tWallet.getWalletAmount() + money.doubleValue());
        tWalletRecord.setType(0);
        tWalletRecord.setBankNumber(bankNumber);
        tWalletRecord.setCreateTime(DateUtils.getDate());
        tWalletRecord.setUpdateTime(DateUtils.getDate());
        tWalletRecordService.insert(tWalletRecord);
        obj.put("status", "Success");
        return obj;

    }

    /**
     * 用户退款
     * @param userId
     * @param walletId
     * @param money
     * @param orderId
     * @return
     */
    @Transactional
    @PostMapping("/refund")
    public Object refund(@RequestParam("userId") long userId,
                         @RequestParam("walletId") long walletId,
                         @RequestParam("money") BigDecimal money,
                         @RequestParam("orderId") Long orderId) {
        Map<String, Object> obj = new HashMap<String, Object>();
        // 用户退款到钱包
        tWalletService.refundToWallet(userId, money);
        // 退款明细
        TWallet tWallet = tWalletService.queryById(userId);
        TWalletRecord tWalletRecord = new TWalletRecord();
        tWalletRecord.setWalletId(walletId);
        tWalletRecord.setUserId(userId);
        tWalletRecord.setOrderId(orderId);
        tWalletRecord.setAmount(tWallet.getWalletAmount());
        tWalletRecord.setOldAmount(tWallet.getWalletAmount() - money.doubleValue());
        tWalletRecord.setType(1);
        tWalletRecord.setCreateTime(DateUtils.getDate());
        tWalletRecord.setUpdateTime(DateUtils.getDate());
        tWalletRecordService.insert(tWalletRecord);
        obj.put("status", "Success");
        return obj;

    }

    /**
     * 通过用户主键查询钱包详情
     * @param userId 主键
     * @return 单条数据
     */
    @GetMapping("/getWallet/{userId}")
    public Object queryById(@PathVariable("userId") Integer userId) {
        TWallet tWallet = tWalletService.queryById(userId);
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("status", "Success");
        obj.put("content", tWallet);
        return obj;
    }


    /**
     * 用户充值
     * @param tWallet 实体
     * @return 新增结果
     */
    @PostMapping
    public Object add(@RequestBody TWallet tWallet) {
        tWalletService.insert(tWallet);
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("status", "Success");
        return obj;
    }



}

